package edu.ccut.saturn.studio.database.model;

import java.util.List;

import org.eclipse.swt.graphics.Image;


import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;

/**
 * 一个数据库实例
 * 
 * @author 谷钰
 * @version 1.0 2008.12.02
 */
public class SqlSchema extends SaturnTree {
	private static final String DBCON_NAME = ""; //$NON-NLS-1$

	private DBConnection dbConnection = null;

	public SqlSchema(String name, DBConnection dbConnection) {
		super(name);

		this.dbConnection = dbConnection;
	}

	@Override
	public void expand() throws Exception {
		if (super.hasChildren()) {
			super.removeChildren();
		}
		
		SqlFolder tableFolder = new SqlFolder(SqlFolder.TABLES, this);

		SqlFolder viewFolder = new SqlFolder(SqlFolder.VIEWS, this);

		super.addChild(tableFolder);
		super.addChild(viewFolder);

		tableFolder.expand();
		viewFolder.expand();
		
		super.setOpen(true);
	}

	@Override
	public Image getImage() {
		return Activator.getImage(Images.DATABASE_SCHEMA);
	}

	public String getDescription() {
		StringBuffer stringbuffer = new StringBuffer();
		stringbuffer
				.append("<b>").append(DBCON_NAME).append("<b>").append(dbConnection.getName()); //$NON-NLS-1$ //$NON-NLS-2$
		return stringbuffer.toString();
	}

	public List<String> getTableNames() throws Exception {
		return this.dbConnection.getTableNames(super.getName());
	}

	public List<String> getViewNames() throws Exception {
		return this.dbConnection.getViewNames(super.getName());
	}

	public List<SqlColumn> getTableColumns(String tableName) throws Exception {
		return this.dbConnection.getTableColumns(super.getName(), tableName);
	}
}
